/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * TransaksiPenjualan.java
 *
 * Created on Aug 11, 2010, 11:28:41 PM
 */
package com.ivanbiz.gui;

import com.ivanbiz.dao.BiayaLainDAO;
import com.ivanbiz.helper.CenterRenderer;
import com.ivanbiz.helper.RightRenderer;
import com.ivanbiz.helper.ServiceHelper;
import com.ivanbiz.impl.BiayaLainDAOImpl;
import com.ivanbiz.model.BiayaLain;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

/**
 *
 * @author Aprisma
 */
public class TransaksiBiayaLain extends javax.swing.JInternalFrame {

    BiayaLainDAO biayaLainDAO = new BiayaLainDAOImpl();
    List<BiayaLain> listBiayaLain = new ArrayList<BiayaLain>();
    NumberFormat numberFormat = NumberFormat.getInstance(Locale.US);
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd MMMM yyyy");

    /** Creates new form TransaksiPenjualan */
    public TransaksiBiayaLain() {
        initComponents();
        updateData();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        groupPilihan = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        labelTanggal = new javax.swing.JLabel();
        textTanggal = new javax.swing.JTextField();
        labelNama = new javax.swing.JLabel();
        textNama = new javax.swing.JTextField();
        labelJumlah = new javax.swing.JLabel();
        textJumlah = new javax.swing.JTextField();
        textKeterangan = new javax.swing.JTextField();
        labelKeterangan = new javax.swing.JLabel();
        buttonProses = new javax.swing.JButton();
        labelPT = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tableBiayaLain = new javax.swing.JTable();
        jPanel3 = new javax.swing.JPanel();
        textTotal = new javax.swing.JTextField();

        setBorder(new org.jdesktop.swingx.border.DropShadowBorder());
        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setTitle("Transaksi Biaya Lain - Lain");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Biaya Lain - Lain"));

        labelTanggal.setFont(new java.awt.Font("Tahoma", 1, 11));
        labelTanggal.setText("Tanggal");

        textTanggal.setEditable(false);
        textTanggal.setFont(new java.awt.Font("Tahoma", 1, 11));
        textTanggal.setText("jTextField1");

        labelNama.setFont(new java.awt.Font("Tahoma", 1, 11));
        labelNama.setText("Nama");

        textNama.setFont(new java.awt.Font("Tahoma", 1, 11));
        textNama.setText("jTextField2");

        labelJumlah.setFont(new java.awt.Font("Tahoma", 1, 11));
        labelJumlah.setText("Jumlah Biaya");

        textJumlah.setFont(new java.awt.Font("Tahoma", 1, 11));
        textJumlah.setText("jTextField1");

        textKeterangan.setFont(new java.awt.Font("Tahoma", 1, 11));
        textKeterangan.setText("jTextField1");

        labelKeterangan.setFont(new java.awt.Font("Tahoma", 1, 11));
        labelKeterangan.setText("Keterangan");

        buttonProses.setFont(new java.awt.Font("Tahoma", 1, 11));
        buttonProses.setText("Prosess");
        buttonProses.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonProsesActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(labelKeterangan)
                            .addComponent(labelTanggal)
                            .addComponent(labelNama)
                            .addComponent(labelJumlah))
                        .addGap(8, 8, 8)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(textTanggal, javax.swing.GroupLayout.DEFAULT_SIZE, 262, Short.MAX_VALUE)
                            .addComponent(textNama, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 262, Short.MAX_VALUE)
                            .addComponent(textJumlah, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 262, Short.MAX_VALUE)
                            .addComponent(textKeterangan, javax.swing.GroupLayout.DEFAULT_SIZE, 262, Short.MAX_VALUE)))
                    .addComponent(buttonProses, javax.swing.GroupLayout.Alignment.TRAILING))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelTanggal)
                    .addComponent(textTanggal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(textNama, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelNama))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(textJumlah, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelJumlah))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(textKeterangan, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelKeterangan))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(buttonProses)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        labelPT.setFont(new java.awt.Font("Tahoma", 1, 40));
        labelPT.setText("PT BERGUNA JAYA");

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabel4.setText("Jln. Jembatan Selatan no 470 Panglima Polim Jakarta Selatan ");

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabel5.setText("Telp : 08174820800");

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Tabel Biaya Lain - Lain"));

        tableBiayaLain.setAutoCreateRowSorter(true);
        tableBiayaLain.setFont(new java.awt.Font("Tahoma", 1, 11));
        tableBiayaLain.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(tableBiayaLain);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 734, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 198, Short.MAX_VALUE)
                .addContainerGap())
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Total"));

        textTotal.setEditable(false);
        textTotal.setFont(new java.awt.Font("Tahoma", 1, 18));
        textTotal.setText("jTextField1");

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(textTotal, javax.swing.GroupLayout.DEFAULT_SIZE, 734, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addComponent(textTotal)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(labelPT)
                            .addComponent(jLabel5)
                            .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 7, Short.MAX_VALUE)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(labelPT)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel5))
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-800)/2, (screenSize.height-550)/2, 800, 550);
    }// </editor-fold>//GEN-END:initComponents

    private void buttonProsesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonProsesActionPerformed
        try {
            BiayaLain biayaLain = new BiayaLain();
            biayaLain.setCreateDate(ServiceHelper.getDateNow());
            biayaLain.setNamaBiaya(textNama.getText().toUpperCase());
            biayaLain.setJumlahBiaya(Double.parseDouble(textJumlah.getText().toUpperCase()));
            biayaLain.setCatatan(textKeterangan.getText().toUpperCase());
            biayaLainDAO.save(biayaLain);
            updateData();
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, "Format Jumlah Biaya Harus Angka Atau Data Tidak Masuk");
            Logger.getLogger(TransaksiBiayaLain.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_buttonProsesActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonProses;
    private javax.swing.ButtonGroup groupPilihan;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel labelJumlah;
    private javax.swing.JLabel labelKeterangan;
    private javax.swing.JLabel labelNama;
    private javax.swing.JLabel labelPT;
    private javax.swing.JLabel labelTanggal;
    private javax.swing.JTable tableBiayaLain;
    private javax.swing.JTextField textJumlah;
    private javax.swing.JTextField textKeterangan;
    private javax.swing.JTextField textNama;
    private javax.swing.JTextField textTanggal;
    private javax.swing.JTextField textTotal;
    // End of variables declaration//GEN-END:variables

    private void updateData() {
        try {
            textTanggal.setText(simpleDateFormat.format(new Date()));
            textNama.setText("");
            textKeterangan.setText("");
            textJumlah.setText("");
            textTotal.setText("");
            listBiayaLain = biayaLainDAO.getDataByEquals(BiayaLain.class, "createDate", ServiceHelper.getDateNow());
            updateTableBiayaLain();
            updateTotal();
        } catch (Exception ex) {
            Logger.getLogger(TransaksiBiayaLain.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void updateTableBiayaLain() {
        String[] judul = {"No", "Nama", "Jumlah Biaya", "Keterangan"};
        Object[][] isi = new Object[listBiayaLain.size()][4];
        int x = 0;
        int no = 0;
        for (BiayaLain biayaLain : listBiayaLain) {
            isi[x][0] = no += 1;
            isi[x][1] = biayaLain.getNamaBiaya();
            isi[x][2] = "Rp " + numberFormat.format(biayaLain.getJumlahBiaya()) + ",00";
            isi[x][3] = biayaLain.getCatatan();
            x++;
        }
        tableBiayaLain.setModel(new DefaultTableModel(isi, judul) {

            @Override
            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return false;
            }
        });
        TableColumn column0 = tableBiayaLain.getColumnModel().getColumn(0);
        column0.setPreferredWidth(50);
        column0.setMaxWidth(50);
        column0.setCellRenderer(new CenterRenderer());
        TableColumn column1 = tableBiayaLain.getColumnModel().getColumn(1);
        column1.setPreferredWidth(300);
        column1.setMaxWidth(300);
        TableColumn column2 = tableBiayaLain.getColumnModel().getColumn(2);
        column2.setPreferredWidth(125);
        column2.setMaxWidth(124);
        column2.setCellRenderer(new RightRenderer());
    }

    private void updateTotal() {
        double total = 0;
        for (BiayaLain biayaLain : listBiayaLain) {
            total += biayaLain.getJumlahBiaya();
        }
        textTotal.setText("Rp " + numberFormat.format(total) + ",00");
    }
}
